/* 算法实现模板 */
import AlgorithmBase from "./AlgorithmBase";
import TDom from "../../../common/TDom";
import { loadDataset, txtToMatrix , mdRender} from "../../../common/Common";

class ForwardAlgForOSP extends AlgorithmBase{
  constructor(){
    super('ForwardAlgForOSP');
    this.timeStep = 3
  }
  initModelUI(){
    var stepLabel = TDom.createLabel(this.domElementList.model,'时序数:','stepLabel',document.getElementById('timeStep'))
    var stepButton = TDom.createNumberInput(this.domElementList.model,()=>{this.timeStep = document.getElementById('timeStep').value;console.log(this.timeStep);},3,1,'timeStep')
    stepButton.min= 1
  }

  initTrainDS (){
    this.pi =[]
    this.A =[]
    this.B =[]
    this.box =[]
    this.ball =[]
    this.pi = [0.2,0.4,0.4]
    this.A = [[0.5,0.2,0.3],[0.3,0.5,0.2],[0.2,0.3,0.5]]
    this.B = [[0.5,0.5],[0.4,0.6],[0.7,0.3]]
    this.box = ['盒子1','盒子2','盒子3','盒子4']
    this.ball = ['红','白']
  }

  openFileSelector(){

  }


  train(){
    var trainMDLog   = "## 观测概率\r\n";    // 训练日志-使用MD编写
    trainMDLog += "|观测值|概率|$b$|"
    var ball = []
    var box = []
    var obList = this.obValue.split(',')
    var aList = []
    var resAList = []
    console.log(obList);
    for(let i =0;i<this.ball.length;i++){ 
      ball.push(this.ball[i])
    }
    for(let i =0;i<this.box.length;i++){ 
      box.push(this.box[i])
    }
    for(let i =0;i<obList.length;i++){
      if(ball.indexOf(obList[i]) == -1)
        return
      else{
        obList[i] = ball.indexOf(obList[i])
      } 
    }
    //初始化
    for(let i =0;i<this.A.length;i++){
      aList.push((this.pi[i]*1000*this.B[i][obList[0]])/1000)
      console.log(aList);
    }
    resAList=  resAList.concat(aList)
    var tempAList =[]
    for(let i =0;i<obList.length-1;i++){
      for(let k =0;k<this.A.length;k++){
        var tempA = 0
        for(let j =0;j<aList.length;j++){
          tempA += aList[j] * 1000 * this.A[j][k] /1000
          if(i==1){
            console.log(tempA,aList[j],this.A[j][k]);
          }
        }
        tempAList[k] = (tempA *  1000 *  this.B[k][obList[i+1]]/1000) .toFixed(5)
        
      }
      aList =[].concat(tempAList)
      console.log(aList);
      resAList= resAList.concat(aList)
    } 
    console.log(resAList);
    mdRender(trainMDLog, this.domElementList.output);
  }

  trainAnimation(){
    
  }

  handleConfirm(){
    this.obValue =document.getElementById('obDiv').value
    this.trainButton.hidden = false;
  }

  initInputUI(){
    const fileSelector      = document.createElement('input');
    // fileSelector.type       = 'file';
    // fileSelector.hidden     = true;
    // fileSelector.onchange   = this.handleFileChange.bind(this);   
    
   
    TDom.createButton(this.domElementList.input, "获取服务器数据", this.initTrainDS.bind(this));
    this.addButton(this.domElementList.input, "使用本地数据", this.openFileSelector.bind(this));
    var obDiv = document.createElement('input')
    obDiv.type = 'text'
    obDiv.id = 'obDiv'
    obDiv.placeholder = '输入观测值'
    this.domElementList.input.appendChild(obDiv)
    this.addButton(this.domElementList.input, "确认观测值", this.handleConfirm.bind(this));
    // this.domElementList.input.appendChild(fileSelector);
    this.trainButton = this.addButton(this.domElementList.input, "生成", this.train.bind(this));
    this.playButton = this.addButton(this.domElementList.input, "训练动画", this.trainAnimation.bind(this));
    this.playButton.hidden = true;
    this.trainButton.hidden = true;
    this.labelTrainTips = this.addLabel(this.domElementList.input, '');
    this.inputDiv       = this.addDiv(this.domElementList.input);

    this.fileSelector  = fileSelector;
  }

  
}


export default ForwardAlgForOSP;